package com.kingdee.pda;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Build;
import android.os.Handler;
import android.speech.tts.TextToSpeech;
import android.support.annotation.RequiresApi;
import android.support.multidex.MultiDex;
import android.widget.Toast;

import com.example.basecommon.utils.App;
import com.example.basecommon.utils.MusicUtils;
import com.example.basecommon.utils.SpeakUtils;
import com.kingdee.pda.plugins.crash.CacheDirectoryManager;
import com.kingdee.pda.plugins.crash.CrashHandler;
import com.example.basecommon.utils.ConstantUtil;
import com.tencent.bugly.Bugly;
import com.tencent.bugly.beta.Beta;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;


/**
 * 自定义application
 */
public class MyApplication extends Application {

    private List<Activity> activityList = new LinkedList<>();
    private static MyApplication mInstance;
    private static final String APPID_KINGDEE = "9588d9c072";
    private static final String APPID_KINGDEE_TEST = "9382e87b02";
    private static MusicUtils musicUtils;

    public static MyApplication getInstance() {
        return mInstance;
    }


    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;
        initMainProcessInfo();

        initTTS();
        App.initVersion(this);

        initBugly();

        initMusicRing();
    }

    /**
     * 初始化bugly
     */
    private void initBugly() {
        Beta.autoInit = true;
        Beta.autoCheckUpgrade = true;
//        Beta.upgradeCheckPeriod = 60 * 1000 * 60 * 12;
//        Beta.initDelay = 3000;
//        Beta.showInterruptedStrategy = true;
//        Beta.enableHotfix = false;
        String packageName = getPackageName();
//        if ("com.kingdee.pda".equals(packageName)) {
//            Bugly.init(getApplicationContext(), APPID_KINGDEE, false);
//        } else if ("com.kingdee.pda.test".equals(packageName)) {
//            Bugly.init(getApplicationContext(), APPID_KINGDEE_TEST, false);
//        }
    }

    /**
     * 初始化主进程信息
     */
    private void initMainProcessInfo() {
        OkHttpClient okHttpClient = new OkHttpClient().newBuilder()
                .connectTimeout(30000, TimeUnit.MILLISECONDS)
                .readTimeout(30000, TimeUnit.MILLISECONDS)
                .writeTimeout(30000, TimeUnit.MILLISECONDS)
                .build();
        OkHttpUtils.initClient(okHttpClient);
//        if (!ConstantUtil.DEBUG) {
//            CrashHandler crashHandler = CrashHandler.getInstance();
//            crashHandler.init(getApplicationContext());
//        }
        CacheDirectoryManager.getInstance().initAppStoragePath(this);
    }


    @Override
    protected void attachBaseContext(Context base) {
//        super.attachBaseContext(LanguageUtil.attachBaseContext(base));
        super.attachBaseContext(base);
        MultiDex.install(base);

    }


    /**
     * 添加页面对象
     *
     * @param activity
     */
    public void addActivity(Activity activity) {
        if (null != activityList) {
            activityList.add(activity);
        }
    }

    /**
     * 关闭所有activity
     */
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    public void finishAllActivity() {
        for (Activity activity : activityList) {
            if (activity != null && !activity.isDestroyed()) {
                activity.finish();
            }
        }
    }

    /**
     * 退出应用
     */
    public void exitApplication() {
        try {
            finishAllActivity();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.exit(0);
        }
    }

    /**
     * 移除activity历史记录
     *
     * @param activity
     */
    public void removeActivity(Activity activity) {
        if (activityList == null || activityList.isEmpty()) {
            return;
        }
        activityList.remove(activity);
    }

    /**
     * 移除指定Activity实例
     *
     * @param activityName
     */
    public void removeActivity(String activityName) {
        if (activityList.size() > 0) {
            for (Activity activity : activityList) {
                if (activity.getClass().getName().equals(activityName) && activity != null && !activity.isDestroyed()) {
                    activity.finish();
                }
            }
        }
    }

    /**
     * 初始化语音播报
     */
    private void initTTS() {
        SpeakUtils.init(getApplicationContext(), new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if (status == TextToSpeech.SUCCESS) {
                    int supported = SpeakUtils.mTTS.setLanguage(Locale.getDefault());
                    if ((supported != TextToSpeech.LANG_AVAILABLE) && (supported != TextToSpeech.LANG_COUNTRY_AVAILABLE)) {
                        Toast.makeText(getApplicationContext(), "不支持该语言", Toast.LENGTH_LONG).show();
                    }
                } else {
                    Toast.makeText(getApplicationContext(), "请安装科大讯飞语音引擎", Toast.LENGTH_LONG).show();
                }
            }
        });
    }

    private void initMusicRing() {
        musicUtils = MusicUtils.getInstance();
    }

    public Context getContext() {
        return getApplicationContext();
    }
}
